package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.domain.CustomerTransfer;
import cn.wolfcode.crm.query.CustomerTransferQueryObject;
import cn.wolfcode.crm.service.ICustomerTransferService;
import cn.wolfcode.crm.util.JSONResult;
import cn.wolfcode.crm.util.UserContext;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/customerTransfer")
public class CustomerTransferController {
    @Autowired
    private ICustomerTransferService transferService;

    @RequestMapping("/list")
    public String list(@ModelAttribute("qo") CustomerTransferQueryObject qo, Model model) {
        Subject subject = SecurityUtils.getSubject();
        if (!(subject.hasRole("admin") || subject.hasRole("CLIENT_MGR"))) {
            qo.setSellerId(UserContext.getCurrentUser().getId());
        }
        qo.setOrderBy("ct.operate_time desc");
        model.addAttribute("pageInfo", transferService.query(qo));
        return "customerTransfer/list";
    }

    @RequestMapping("/save")
    @ResponseBody
    public Object save(CustomerTransfer entity) {
        transferService.save(entity);
        return new JSONResult();
    }
}
